package game.database.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class CommonDB {
	private static final Logger logger = Logger.getLogger(CommonDB.class);
	
	/**
	 * get database Conneciton
	 * 
	 * @return
	 */
	public static Connection getConnection() {	
		return DbUtil.getConnection();

//		return DbUtil.getConnectionMysql();//
//		return DbUtil.getConnection();//
		//return DbUtil.getConnectionMysql();//
	}
	/**
	 * Close Data operation OBJ
	 * 
	 * @param conn
	 * @param stmt
	 * @param ps
	 * @param rs
	 */
	public static void destroyConn(Connection conn, Statement stmt,
			PreparedStatement ps, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception e) {
			logger.error(e);
		}
		rs = null;
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception e) {
			logger.error(e);
		}
		stmt = null;
		try {
			if (ps != null)
				ps.close();
		} catch (Exception e) {
			logger.error(e);
		}
		ps = null;
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			logger.error(e);
			logger.info("CommonDB===destroyConn===="+e.toString());
		}
		conn = null;
	}
	/**
	 * check get boolean result
	 * @param strSql
	 * @return
	 */
	public boolean executeQuery(String strSql){
		boolean bln = false;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(strSql);
			rs = ps.executeQuery();
			if (rs.next())
               bln = true;
		} catch (Exception e) {
			bln = false;
			logger.error(e);
			logger.info("CommonDB===executeQuery===="+e.toString());
		}finally{
			destroyConn(null,null,ps,rs);
			conn = null;
			ps = null;
			rs = null;
		}
		return bln;
	}
	/**
	 * 
	 * @param strSql
	 * @return
	 */
	public boolean executeUpdate(Connection conn,String strSql){
		boolean bln = false;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(strSql);
			int xx = ps.executeUpdate();
			if (xx>0)
               bln = true;
		} catch (Exception e) {
			bln = false;
			logger.error(e.toString());
			logger.info("CommonDB===executeUpdate===="+e.toString());
		}finally{
			destroyConn(null,null,ps,null);
			conn = null;
			ps = null;
		}
		return bln;		
	}
	public String getStrInfo(Connection conn,String sql) {
		String str = null;
		PreparedStatement ps = null;		
		ResultSet rs = null;
		//logger.info(sql);
		try{
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
				str = rs.getString(1);
			if(str == null|| str.equals("")){
				str = "";
			}
		}catch(Exception e){
			logger.info("CommonDB===getStrInfo===="+e.toString());
			logger.error(e.toString());
		}finally{
			destroyConn(null, null, ps, rs);
			rs = null;
			ps = null;
		}
		//logger.info(str);
		return str;
	}	
	public int getIntInfo(Connection conn,String sql) {
		PreparedStatement ps = null;		
		ResultSet rs = null;
		int count = 0;
		try{
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
				count = rs.getInt(1);
			
		}catch(Exception e){
			logger.info("CommonDB===getIntInfo===="+e.toString());
			logger.error(e.toString());
		}finally{
			destroyConn(null, null, ps, rs);
			rs = null;
			ps = null;
		}
		return count;
	}	
	
	public static int getNextID(String seqName) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		int xid = 0;
		Connection conn = null;
		try {			
			conn = getConnection();
			//sql.append("select SEQ_tnpcinfo.nextval from dual");
			sql.append("select nextval('"+seqName+"')");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			if (rs.next()) {
				xid = rs.getInt(1); 
			}			
		} catch (Exception e) {
			logger.info("CommonDB===getNextID===="+e.toString());
			logger.error(e.toString());
		} finally {
			destroyConn(conn, null, ps, rs);
			rs = null;
			ps = null;
			conn = null;
		}
		return xid;
	}
}
